<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" destroyOnClose showFooter :title="title" :width="700" @ok="handleSubmit">
    <BasicForm @register="registerForm"></BasicForm>
  </BasicDrawer>
</template>
<script lang="ts" setup>
import { ref } from 'vue';
import { BasicForm, useForm } from '@apps/antd-admin/src/components/Form/index';
import { BasicDrawer, useDrawerInner } from '@apps/antd-admin/src/components/drawer';
import { commonUseformSchema } from '../data/cardserver.data';
import { Api } from '@apps/ant-bus/src/views/business/userManage/cardserverRecord/data/api';

const emit = defineEmits(['success', 'register']);
//计算标题
const title = ref('');
const [registerForm, { resetFields, setFieldsValue, validate, updateSchema, saveOrUpdate }] = useForm({
  labelWidth: 150,
  labelCol: { span: 4 },
  schemas: commonUseformSchema,
  rowKey: 'cardserverRecordId',
  saveApi: Api.save,
  updateApi: Api.update,
  saveHandler: 'cardserverCommonUseHandler',
  showActionButtonGroup: false,
});

const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
  let record = data.record;
  title.value = record.serverName;
  resetFields();
  setDrawerProps({ confirmLoading: false });
  let userGoodsId = record.userGoodsId;
  let cardserverId = record.cardserverId;
  let newData = {
    cardserverId: cardserverId,
    userGoodsId: userGoodsId,
    optType: record.optType,
  };
  setFieldsValue({
    ...newData,
  });
});

async function handleSubmit() {
  try {
    setDrawerProps({ confirmLoading: true });
    await saveOrUpdate(emit);
    closeDrawer();
  } finally {
    setDrawerProps({ confirmLoading: false });
  }
}
</script>
